#include "Step.hh"
void Step(TFile* file, char* name, char* OutputFile, double min, double max, double pos, double hight, double diff, char* hfile){
  //TCanvas *c1 = new TCanvas;
  //c1->cd();
  TH1F *reb4;
  TH1F *h;

  Double_t par[3];
  par[0]=pos;
  par[1]=hight;
  par[2]=diff;

  ofstream output(OutputFile, std::ios::out | std::ios::app);
  h=(TH1F*) file->Get(name);
  reb4 = (TH1F*)h->Rebin(4);
  reb4->GetXaxis()->SetRangeUser(min,max); 

  //h->GetXaxis()->SetRangeUser(min,max); 
  //h->Draw();
  TF1 *stepf = new TF1("stepf",fErfc,min,max,3);
  stepf->SetParameters(par);
  stepf->SetLineColor(3);
  stepf->SetLineWidth(1);
  stepf->SetParLimits(2,par[2]*0.5,par[2]*2);
  reb4->Fit("stepf");
  //h->Fit("stepf");

  output << setprecision(5) << 1/stepf->GetParameter(0) << "\t" << stepf->GetParameter(1) << "\t" << setprecision(3) << stepf->GetParameter(2) << "\t" << setprecision(5) << stepf->GetChisquare() << "\t" << name << endl;
  //c1->SaveAs("test.ps");
  TFile* HFile = new TFile(hfile,"update");
  if(strstr(name,"Back"))
    reb4->GetXaxis()->SetRangeUser(0.3,0.9);
  else if(strstr(name,"For"))
    reb4->GetXaxis()->SetRangeUser(0.2,2.0);
  reb4->Write("",TObject::kOverwrite);
  //h->Write("",TObject::kOverwrite);
  stepf->Write("",TObject::kOverwrite);
  HFile->Close();

}
Double_t fErfc(Double_t *x, Double_t *par){
  if(par[2] != 0)
    return par[1]/2.*TMath::Erfc((x[0] - par[0])/par[2]);
  else
    return 0;
}
